package bupt.party.online.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import bupt.party.online.video.Video;
import bupt.party.online.video.VideoMgr;

import com.opensymphony.xwork2.ActionSupport;

public class DetailsAction  extends Pagination implements ServletRequestAware,ServletResponseAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	HttpServletRequest request;
	HttpServletResponse response;
	
	private String paginationCode;

	public String execute(){
		
		return "success";
	}
	
	public String details(){
		String categoryId = request.getParameter("categoryId");
		if(categoryId == null)
			categoryId = "/";
		
		VideoMgr mgr = VideoMgr.getInstance();
		List<Video> videos =  mgr.getVideos(categoryId,  Video.STATUS_PUBLISHED);
		if(this.pageNow == null)
			pageNow = 1;
		totalResults = 0;
		paginationCode = "";
		totalResults =	videos.size();
		this.pageCount = (totalResults%pageSize == 0)?totalResults/pageSize : totalResults/pageSize + 1;
		if(totalResults > 0) {
			paginationCode = Pagination.getPaginationCode(pageSize, totalResults, pageNow);
		} else {
			paginationCode = "没有查询到有效数据！";
		}
		// 开始分页
		int fromIndex = (pageNow - 1)*pageSize;
		int toIndex = fromIndex + pageSize;
		toIndex = toIndex > videos.size() ? videos.size() : toIndex;
		request.setAttribute("videos", videos.subList(fromIndex, toIndex));
		request.setAttribute("categoryId", categoryId);
//		System.out.println("categoryId:" + categoryId + "  ::videos:" + videos);
		return "details";
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}
	
	public String getPaginationCode() {
		return paginationCode;
	}
	public void setPaginationCode(String paginationCode) {
		this.paginationCode = paginationCode;
	}
}
